<template>
  <el-card>
    <!-- 1.0 标题 -->
    <template #header size="mini">
      <div class="fen">
        <h1>商品分类</h1>
        <div>
          <el-button type="primary" size="mini" @click="dialogVisible = true"
            >添加商品</el-button
          >
        </div>
      </div>
    </template>
    <!-- 2.0 内容 -->
    <el-table :data="tableData" style="width: 100%" size="mini">
      <el-table-column prop="id" label="序号" width="80"> </el-table-column>
      <el-table-column label="分类名称">
        <template v-slot="{ row }">
          <span v-if="row.flag">{{ row.cateName }}</span>
          <el-input v-else v-model="row.cateName" size="mini"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="是否启用">
        <template v-slot="{ row }">
          <el-switch
            v-model="row.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            :disabled="row.flag"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            size="mini"
            :type="row.flag ? 'primary' : 'success'"
            @click="editGoods(row, $event)"
            >{{ row.flag ? '编辑' : '完成' }}</el-button
          >
          <el-button size="mini" type="danger" @click="delet(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 3.0 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 3, 4, 7]"
      :page-size="pageSize"
      :total="total"
    >
    </el-pagination>
    <!-- 添加 -->
    <el-dialog title="商品的添加" :visible.sync="dialogVisible" width="30%">
      <el-form
        size="mini"
        :model="ruleForm"
        status-icon
        ref="ruleForm"
        label-width="80px"
        width="80%"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cateName">
          <el-input
            type="text"
            v-model="ruleForm.cateName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="state">
          <el-switch
            v-model="ruleForm.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="query" size="mini">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { goodcatelist, addcate, delcate, editcate } from '../../api/goods.js'
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 3,
      total: 0,
      tableData: [],
      ruleForm: {
        cateName: '',
        state: true
      },
      dialogVisible: false
    }
  },
  methods: {
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.list()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.list()
    },
    /* 渲染数据 */
    async list() {
      const data = await goodcatelist({
        currentPage: parseInt(this.currentPage),
        pageSize: parseInt(this.pageSize)
      })
      // 为了编辑的flag，所以在这个地方的作用就是每行都有一个标杆
      data.data.forEach(item => {
        item.flag = true
      })
      // console.log(data)
      this.tableData = data.data
      this.total = data.total
    },
    /* 添加 */
    async query() {
      const { code, msg } = await addcate({
        cateName: this.ruleForm.cateName,
        state: this.ruleForm.state
      })
      if (code === 0) {
        this.ruleForm.cateName = ''
      }
      this.dialogVisible = false
    },
    /* 删除 */
    delet(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code, msg } = await delcate({ id })

          if (code === 0) {
            this.list()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    /* 编辑 */
    async editGoods(row, event) {
      const txt = event.currentTarget.innerText
      row.flag = !row.flag
      if (txt === '完成') {
        const { code, msg } = await editcate({
          id: row.id,
          cateName: row.cateName,
          state: Boolean(row.state)
        })
        if (code === 0) {
          this.$message({
            message: msg,
            type: 'success'
          })
        }
      }
    }
  },
  created() {
    this.list()
  },
  watch: {
    total() {
      if (
        this.total === (this.currentPage - 1) * this.pageSize &&
        this.total !== 0
      ) {
        this.currentPage -= 1
        this.list()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.fen {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
</style>
